package com.warframe.mod;

import com.tools.HibernateUtils;
import com.tools.StringContent;
import com.warframe.entity.database.WarframeMarketKeyWordEO;
import com.warframe.entity.message.Message;
import com.warframe.message.Executor;
import org.hibernate.SQLQuery;
import org.hibernate.Session;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Mod implements Runnable {

    private final static String sql = "select * from WarframeMarketKeyWord a where a.item_cn like '%TARGET%' order by a.id";
    private Message message;
    private List<String> msg;

    public Mod(Message message) {
        this.message = message;
    }

    private void getResult() {
        Session session = HibernateUtils.getSessionFactory().openSession();
        SQLQuery sqlQuery = session.createSQLQuery(sql.replace("TARGET", message.getMsg()));
        List<WarframeMarketKeyWordEO> warframeMarketKeyWordEOList = sqlQuery.addEntity(WarframeMarketKeyWordEO.class).list();
        if (warframeMarketKeyWordEOList.size() > 0) {
            msg = Arrays.asList(warframeMarketKeyWordEOList.get(0).getRecommendMod().split("\\|"));
        } else {
            msg = new ArrayList<>();
            msg.add("暂无此武器或战甲配卡");
        }
        session.close();
    }

    @Override
    public void run() {
        getResult();
        for (String eachStr : msg) {
            Message eachMsg = new Message();
            eachMsg.setFromQQ(message.getFromQQ());
            eachMsg.setMsg(eachStr);
            eachMsg.setSign(StringContent.SENDPERSON);
            Executor.getInstance().pushMsg(eachMsg);
        }
    }
}
